package belf.migrate.api.taskconf;

import belf.migrate.api.common.JobMode;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;
import java.util.List;

/**
 * 定义一个可执行任务。任务包含对应的配置信息{@link belf.migrate.api.taskconf}、任务类型、工作模式、遇错执行策略、
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Task {
    private long taskId;
    private String name;         //任务名称
    private String desc;         //任务的描述
    private String owner;        //任务所属用户。满足多租户管理需求
    private Date createDate;     //任务创建时间

    private TaskConf taskConf;   //执行任务需要的配置信息，作为外键引用表migration_task_conf

    private TaskType taskType;   //任务类型
    /**
     * 本次任务由哪些子任务组成？比如"CREATE_TYPE,CREATE_TABLE,CREATE_VIEW,CREATE_TRIGGER".<br/>
     * 注意：本参数仅当{@link #taskType}为{@link TaskType#SYNC_STRUCTURE}时才起作用
     */
    private String taskTypeScope; //

    private JobMode jobMode;     //任务的工作模式

    private TaskPolicy taskPolicy = TaskPolicy.ERROR_STOP;

    public enum TaskPolicy {
        ERROR_STOP("ERROR_STOP", "遇到错误即停止任务"),
        ERROR_CONTINUE("ERROR_CONTINUE", "遇到错误继续执行任务");

        private final String name;
        private final String message;

        TaskPolicy(String name, String message) {
            this.name = name;
            this.message = message;
        }

        public String getName() {
            return name;
        }

        public String getMessage() {
            return message;
        }
    }

    public enum TaskType {
        SYNC_STRUCTURE("SYNC_STRUCTURE", "同步数据库结构"),//包含同步表、视图、Type、Class、触发器、存储过程等结构
        DROP_STRUCTURE("DROP_STRUCTURE", "删除目标库所有结构"),
        SYNC_DATA("SYNC_DATA", "同步数据"),
        CREATE_TABLE("CREATE_TABLE", "创建表"),
        DROP_TABLE("DROP_TABLE", "删除表"),
        CREATE_FOREIGNKEY("CREATE_FOREIGNKEY", "创建视图"),
        CREATE_VIEW("CREATE_VIEW", "创建视图"),
        DROP_VIEW("DROP_VIEW", "删除视图"),
        CREATE_TYPE("CREATE_TYPE", "创建类型"),
        DROP_TYPE("DROP_TYPE", "删除类型"),
        CREATE_SEQ("CREATE_SEQ", "创建序列"),
        DROP_SEQ("DROP_SEQ","删除序列"),
        CREATE_TRIGGER("CREATE_TRIGGER", "创建触发器"),
        DROP_TRIGGER("DROP_TRIGGER", "删除触发器"),
        CREATE_FUNCTION("CREATE_FUNCTION", "创建函数"),
        DROP_FUNCTION("DROP_FUNCTION", "删除函数"),
        CREATE_PROCEDURE("CREATE_PROCEDURE", "创建存储过程"),
        DROP_PROCEDURE("DROP_PROCEDURE", "删除存储过程"),
        CREATE_SYNONYM("CREATE_SYNONYM", "创建同义词"),
        DROP_SYNONYM("DROP_SYNONYM", "删除同义词"),

        SYNC_SPECIAL_ROWS("SYNC_SPECIAL_ROWS", "同步特殊字段，通过Groovy等脚本读源库表字段，写目标库表字段"),
        JDBC_SYNC_DATA("JDBC_SYNC_DATA", "JDBC方式同步数据"),

        EVALUATE_MIGRATION("EVALUATE_MIGRATION", "迁移任务评估"),
        EVALUATE_TABLE("EVALUATE_TABLE", "表评估"),

        EVALUATE_VIEW("EVALUATE_VIEW", "视图评估"),
        EVALUATE_PROCEDURE("EVALUATE_PROCEDURE", "存储过程评估"),
        EVALUATE_TRIGGER("EVALUATE_TRIGGER", "触发器评估"),

        EVALUATE_TYPE("EVALUATE_TYPE", "类型评估"),

        EVALUATE_INDEX("EVALUATE_INDEX", "索引评估"),

        EVALUATE_SEQ("EVALUATE_SEQ", "序列评估"),

        EVALUATE_SYNONYM("EVALUATE_SYNONYM", "同义词评估"),

        EVALUATE_FUNC("EVALUATE_FUNC", "函数评估"),
        VERIFY_MIGRATION("VERIFY_MIGRATION","迁移结果校验"),
        VERIFY_TABLE("VERIFY_TABLE","表校验"),
        VERIFY_VIEW("VERIFY_VIEW","视图校验"),
        VERIFY_TRIGGER("VERIFY_TRIGGER","触发器校验"),

        VERIFY_INDEX("VERIFY_INDEX","索引校验"),

        VERIFY_SEQ("VERIFY_SEQ","序列校验"),

        VERIFY_SYNONYM("VERIFY_SYNONYM","同义词校验"),

        VERIFY_TYPE("VERIFY_TYPE","类型校验"),

        VERIFY_FUNC("VERIFY_FUNC","函数校验"),
        EXECUTE_SQL("EXECUTE_SQL", "执行SQL");

        private final String name;  //在mysql表migration_task中TaskType保存的字符串形式（是一个enum字段）
        private final String desc;  //描述信息，用于web console展示

        TaskType(String name, String desc) {
            this.name = name;
            this.desc = desc;
        }

        public String getName() {
            return name;
        }

        public String getDesc() {
            return desc;
        }

        public static List<TaskType> getVerifyTaskTypes() {
            return List.of(VERIFY_TABLE,VERIFY_VIEW,VERIFY_TRIGGER,VERIFY_SEQ,VERIFY_SYNONYM,VERIFY_TYPE,VERIFY_FUNC);
        }

    }

}

/* 建表语句
CREATE TABLE migration_task(
    task_id INT AUTO_INCREMENT PRIMARY KEY,
    task_name VARCHAR(128),
    task_desc VARCHAR(255),
    owner VARCHAR(64),
    create_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    task_conf_id INT,
    task_type ENUM('SYNC_STRUCTURE', 'SYNC_DATA', 'CREATE_TABLE', 'DROP_TABLE',
      'CREATE_FOREIGNKEY',
      'CREATE_VIEW', 'DROP_VIEW',
      'CREATE_TYPE', 'DROP_TYPE',
      'CREATE_TRIGGER', 'DROP_TRIGGER',
      'CREATE_PROCETURE', 'DROP_PROCETURE'
    ),
    job_mode ENUM('ONE_TIME', 'BATCH', 'STREAMING'),
    task_policy ENUM('ERROR_STOP', 'ERROR_CONTINUE')
);
 */